﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundService2Ref;

    public class WaveFlowDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chkIsActive;
        protected DropDownList ddlWaveStep;
        protected HtmlForm F7163;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnWaveFlowID;
        protected HtmlHead Head1;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblWaveFlowDetail;
        protected Label lblWaveStep;
        protected HtmlLink mycss;
        protected TextBox txtLineID;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("WaveFlow.aspx?from={0}&&ID={1}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.hdnWaveFlowID.Value)));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            if (!string.IsNullOrEmpty(this.ddlWaveStep.SelectedValue))
            {
                try
                {
                    success = this.Insert0rUpdateOrderDetail();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7100_030") + ex.Message;
                    return;
                }
            }
            else
            {
                success = true;
            }
            if (success)
            {
                base.Response.Redirect(string.Format("WaveFlow.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "2", base.UrlEncode(this.hdnWaveFlowID.Value)), false);
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                success = this.Insert0rUpdateOrderDetail();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_028") + ex.Message;
                return;
            }
            if (success)
            {
                this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("F7100_011");
                this.hdnLineID.Value = (int.Parse(this.hdnLineID.Value) + 10).ToString();
                this.txtLineID.Text = this.hdnLineID.Value;
                this.hdnFlag.Value = "0";
            }
        }

        private bool Insert0rUpdateOrderDetail()
        {
            bool success = false;
            WaveFlowDetailInfo waveFlowDetailInfo = null;
            if (base.DataEntityViewState != null)
            {
                waveFlowDetailInfo = base.DataEntityViewState as WaveFlowDetailInfo;
            }
            else
            {
                waveFlowDetailInfo = new WaveFlowDetailInfo();
            }
            waveFlowDetailInfo.WaveFlowID = this.hdnWaveFlowID.Value;
            waveFlowDetailInfo.LineID = int.Parse(this.txtLineID.Text);
            waveFlowDetailInfo.WaveStep = this.ddlWaveStep.SelectedValue;
            waveFlowDetailInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            if (this.hdnFlag.Value == "1")
            {
                waveFlowDetailInfo.UpdatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(OutboundService2ClientProxy.GetInstance().UpdateWaveFlowDetailInfo(waveFlowDetailInfo)))
                {
                    success = true;
                }
            }
            else
            {
                waveFlowDetailInfo.CreatedBy = base.UserInfo.UserID;
                waveFlowDetailInfo.UpdatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(OutboundService2ClientProxy.GetInstance().InsertWaveFlowDetailInfo(waveFlowDetailInfo)))
                {
                    success = true;
                }
            }
            base.DataEntityViewState = waveFlowDetailInfo;
            return success;
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlWaveStep, "WaveSteps");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                string WaveFlowID = base.Request.QueryString["ID"];
                this.hdnWaveFlowID.Value = WaveFlowID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"]))
            {
                string LineID = base.Request.QueryString["SeqNO"];
                this.hdnLineID.Value = LineID;
            }
            if (!(string.IsNullOrEmpty(base.Request.QueryString["type"]) || !(base.Request.QueryString["type"] == "Edit")))
            {
                this.hdnFlag.Value = "1";
                this.btnSaveContinue.Enabled = false;
                this.txtLineID.Text = this.hdnLineID.Value;
                this.setWaveFlowDetail2From();
            }
            else
            {
                this.txtLineID.Text = (int.Parse(this.hdnLineID.Value) + 10).ToString();
                this.hdnLineID.Value = this.txtLineID.Text;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void setWaveFlowDetail2From()
        {
            if ((!string.IsNullOrEmpty(this.hdnWaveFlowID.Value) && !string.IsNullOrEmpty(this.hdnLineID.Value)) && (this.hdnLineID.Value != "0"))
            {
                WaveFlowDetailInfo waveFlowDetailInfo = OutboundService2ClientProxy.GetInstance().GetWaveFlowDetailInfoByIDLineID(this.hdnWaveFlowID.Value, this.hdnLineID.Value);
                if (waveFlowDetailInfo != null)
                {
                    this.txtLineID.Text = waveFlowDetailInfo.LineID.ToString();
                    this.ddlWaveStep.Items.Insert(0, new ListItem(waveFlowDetailInfo.WaveStep, ""));
                    this.chkIsActive.Checked = waveFlowDetailInfo.IsActive == "Y";
                    base.DataEntityViewState = waveFlowDetailInfo;
                }
            }
        }
    }
}

